package com.infisight.hudprojector.fragment;

import com.google.gson.Gson;
import com.infisight.hudprojector.R;
import com.infisight.hudprojector.data.SpeechVoiceData;
import com.infisight.hudprojector.data.VoiceDataClass;
import com.infisight.hudprojector.kdxfspeech.SpeechVoiceCompound;
import com.infisight.hudprojector.kdxfspeech.SpeechVoiceRecognition;
import com.infisight.hudprojector.service.MusicService;
import com.infisight.hudprojector.util.CommonUtil;
import com.infisight.hudprojector.util.Constants;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

/**
 * @author hao 首页fragment
 * 
 */
public class HomePJFragment extends Fragment implements OnClickListener {

	public String TAG = "HomePJFragment";
	ImageView fh_im_home_navi, fh_im_home_obd, fh_im_home_speech,
			fg_im_home_music, fg_im_home_phone;
	SpeechVoiceCompound svc;
	SpeechVoiceRecognition svr;
	Gson gson = null;
	SharedPreferences sp;

	public static HomePJFragment newInstance() {
		HomePJFragment fragment = new HomePJFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public HomePJFragment() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		init();
		initClickListener();
		sp = getActivity().getSharedPreferences("bg_music_info", 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		return inflater.inflate(R.layout.fragment_home, container, false);
	}

	/**
	 * 初始化AMap对象
	 */
	private void init() {

		fh_im_home_navi = (ImageView) getActivity().findViewById(
				R.id.fh_im_home_navi);// 导航

		fh_im_home_obd = (ImageView) getActivity().findViewById(
				R.id.fh_im_home_obd);// obd

		fh_im_home_speech = (ImageView) getActivity().findViewById(
				R.id.fh_im_home_speech);// 语音

		fg_im_home_music = (ImageView) getActivity().findViewById(
				R.id.fg_im_home_music);// 音乐

		fg_im_home_phone = (ImageView) getActivity().findViewById(
				R.id.fg_im_home_phone);// 电话
	}

	/**
	 * 初始化相关的按钮事件
	 */
	private void initClickListener() {
		fh_im_home_navi.setOnClickListener(this);
		fh_im_home_obd.setOnClickListener(this);
		fh_im_home_speech.setOnClickListener(this);
		fg_im_home_music.setOnClickListener(this);
		fg_im_home_phone.setOnClickListener(this);
	}

	BroadcastReceiver homePjReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			gson = new Gson();
			// Log.i(TAG,
			// "action != null && Constants.MAIN_HOME_ACTION.equals(action");
			final String action = intent.getAction();
			// 进入导航界面
			if (action != null && Constants.MAIN_NAVI_ACTION.equals(action)) {

				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_NAVI_ACTION),
						SpeechVoiceData.class);
				svd.setCommand(Constants.MAIN_NAVI_COM);
				Log.i(TAG, intent.getStringExtra(Constants.MAIN_NAVI_ACTION));
				CommonUtil.processBroadcast(getActivity(), svd);
			}
			// 显示电话界面
			else if (action != null
					&& Constants.MAIN_PHONE_ACTION.equals(action)) {

				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_PHONE_ACTION),
						SpeechVoiceData.class);
				VoiceDataClass vdc = gson.fromJson(svd.getValue(),
						VoiceDataClass.class);

				svd.setCommand(Constants.MAIN_PHONE_COM);
				svd.setValue(vdc.getValue());
				CommonUtil.processBroadcast(getActivity(), svd);
			}
			// 显示obd
			else if (action != null && Constants.MAIN_OBD_ACTION.equals(action)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_OBD_ACTION),
						SpeechVoiceData.class);

				svd.setCommand(Constants.MAIN_OBD_COM);

				Log.i(TAG, intent.getStringExtra(Constants.MAIN_OBD_ACTION));
				CommonUtil.processBroadcast(getActivity(), svd);
			}
			// 音乐功能
			else if (action != null
					&& Constants.MAIN_MUSIC_ACTION.equals(action)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_MUSIC_ACTION),
						SpeechVoiceData.class);
				VoiceDataClass vdc = gson.fromJson(svd.getValue(),
						VoiceDataClass.class);
				Intent intent2 = new Intent(getActivity(), MusicService.class);
				// 停止播放
				if (vdc.getType().equals(Constants.F_R_MUSIC_STOP_PLAY[0])) {
					intent2.putExtra("play", "pause");
					getActivity().startService(intent2);
				}
				// 继续播放
				else if (vdc.getType().equals(
						Constants.F_R_MUSIC_CONTINUE_PLAY[0])) {
					intent2.putExtra("play", "playing");
					intent2.putExtra("id", sp.getInt("currentMusic", 0));
					getActivity().startService(intent2);
				}
				// 下一首
				else if (vdc.getType().equals(
						Constants.F_R_MUSIC_NEXT_MUSIC_PLAY[0])) {
					intent2.putExtra("play", "next");
					intent2.putExtra("id", sp.getInt("currentMusic", 0) + 1);
					getActivity().startService(intent2);
				}
				// 上一首
				else if (vdc.getType().equals(
						Constants.F_R_MUSIC_LAST_MUSIC_PLAY[0])) {
					int index = sp.getInt("currentMusic", 0) - 1;
					if (index < 0) {
						index = 0;
					}
					intent2.putExtra("play", "last");
					intent2.putExtra("id", index);
					getActivity().startService(intent2);
				} else if (vdc.getType().equals(Constants.F_R_MUSIC_ON[0])) {
					svd.setCommand(Constants.MAIN_MUSIC_COM);

					Log.i(TAG,
							intent.getStringExtra(Constants.MAIN_MUSIC_ACTION));
					CommonUtil.processBroadcast(getActivity(), svd);
				}
			}
		}

	};

	/**
	 * 广播过滤器
	 */
	private static IntentFilter makeNewMsgIntentFilter() {
		final IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constants.MAIN_NAVI_ACTION);
		intentFilter.addAction(Constants.MAIN_PHONE_ACTION);
		intentFilter.addAction(Constants.MAIN_OBD_ACTION);
		intentFilter.addAction(Constants.MAIN_MUSIC_ACTION);

		return intentFilter;
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onResume() {
		getActivity()
				.registerReceiver(homePjReceiver, makeNewMsgIntentFilter());
		super.onResume();
		svr = new SpeechVoiceRecognition(getActivity(), "", null);// 语音识别
		svc = SpeechVoiceCompound.getInstance(getActivity(), "", svr);// 语音合成
		svc.startSpeaking(Constants.F_C_WHAT_YOU_DO);

	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onPause() {
		super.onPause();
		getActivity().unregisterReceiver(homePjReceiver);
		if (svc != null)
			svc.destroy();
		if (svr != null)
			svc.destroy();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		SpeechVoiceData svd = new SpeechVoiceData();
		switch (v.getId()) {
		case R.id.fh_im_home_navi:
			svd.setCommand(Constants.MAIN_NAVI_COM);
			svd.setValue(Constants.CONTROL_MAIN_KEY);
			CommonUtil.processBroadcast(getActivity(), svd);
			break;
		case R.id.fh_im_home_obd:
			svd.setCommand(Constants.MAIN_OBD_COM);
			svd.setValue(Constants.CONTROL_MAIN_KEY);
			CommonUtil.processBroadcast(getActivity(), svd);
			break;
		case R.id.fg_im_home_music:
			svd.setCommand(Constants.MAIN_MUSIC_COM);
			svd.setValue(Constants.CONTROL_MAIN_KEY);
			CommonUtil.processBroadcast(getActivity(), svd);
			break;
		case R.id.fg_im_home_phone:
			svd.setCommand(Constants.MAIN_PHONE_COM);
			svd.setValue(Constants.CONTROL_MAIN_KEY);
			CommonUtil.processBroadcast(getActivity(), svd);
			break;
		}
	}
}
